--
-- Author:      feilong
-- DateTime:    2019-12-16 17:19:33
-- Description: 服务管理

require "skynet.manager"
local skynet = require "skynet"
local Hotfix = require("Hotfix")
local MessageDispatch = import("MessageDispatch")
local Config = import("ActConfig")
local ActivityManager = import("manager.ActivityManager")
local MessageHandler = import("manager.MessageHandler")



--此服务唯一全局变量, 
global = {}
global.hotfix = Hotfix.new()
--禁止出现别的全局变量
local function disable_global()
	local __g = _G
	setmetatable(global, {
	    __newindex = function(_, name, value)
	        rawset(__g, name, value)
	    end,
	    __index = function(_, name)
	        return rawget(__g, name)
	    end
	})
	--防止手误写出全局变量
	setmetatable(__g, {
	    __newindex = function(_, name, value)
	        local msg = "USE 'global.%s = value' INSTEAD OF SET GLOBAL VARIABLE"
	        error(string.format(msg, name), 0)
	    end,
	})
end

local function init()

	local message_dispatch = MessageDispatch.new()	
	local message_handler = MessageHandler.new(message_dispatch)
	local config = Config.new()
	local act_manager = ActivityManager.new()

	global.message_dispatch = message_dispatch
	global.message_handler = message_handler
	global.config = config
	global.act_manager = act_manager

	skynet.register_protocol({
		name = "client",
		id = skynet.PTYPE_CLIENT,
		pack = skynet.pack,
		unpack = skynet.unpack,
	})
	--消息派发
	local func = message_dispatch:dispatchClient()	
	skynet.dispatch("client", func)
	skynet.dispatch("lua", message_dispatch:dispatchLua())	
end

---------------------------------------------------------
-- skynet
---------------------------------------------------------
disable_global()
init()
skynet.start(function()
	skynet.register('.manager')
end)


--